package sk.tramtatiri.pages.users;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import sk.tramtatiri.MySession;
import sk.tramtatiri.data.dao.interfaces.InOutHelperDao;
import sk.tramtatiri.data.dao.interfaces.RecipeDao;
import sk.tramtatiri.data.dao.interfaces.UserDao;
import sk.tramtatiri.data.dao.interfaces.UserFoodDao;
import sk.tramtatiri.data.dao.interfaces.UserRecipeDao;
import sk.tramtatiri.data.dataobjects.InOutHelper;
import sk.tramtatiri.data.dataobjects.Recipe;
import sk.tramtatiri.data.dataobjects.UserRecipe;

public class RecipeDetailPage extends UserPageTemplate {
	
	@SpringBean
	private RecipeDao recipeDao;
	@SpringBean
	private UserDao userDao;
	@SpringBean
	private UserRecipeDao userRecipeDao;
	
	@SpringBean
	private UserFoodDao userFoodDao;
	
	@SpringBean
	private InOutHelperDao inOutHelperDao;
	private Recipe recipe;

	public RecipeDetailPage(PageParameters pageParameters) {
		FeedbackPanel feedback = new FeedbackPanel("feedback");
		feedback.setOutputMarkupId(true);
		add(feedback);
		
		String name = null;
		if(!pageParameters.isEmpty()){
			name = (String) pageParameters.get("name");
		}
		recipe = recipeDao.findName(name);

		Form<?> form = new Form<Void>("formRecipe");
		form.add(new Label("recipeName", Model.of(recipe.getName())));
		
		form.add(new ListView("row", Arrays.asList(recipe.getIngredients())) {
			private static final long serialVersionUID = -3431202361881510851L;

			@Override
			protected void populateItem(ListItem item) {
				final String ingredient = (String) item.getModelObject();
				
				PageParameters parameters = new PageParameters();
				parameters.put("name", ingredient);
				BookmarkablePageLink bpl = new BookmarkablePageLink("linkToFood", FoodDetailPage.class, parameters);
				bpl.add(new Label("foodName", Model.of(ingredient)));
				item.add(bpl);
				item.add(new Label("foodWeight", Model.of(recipe.getWeights()[item.getIndex()])));
			}
		});
		
		form.add(new Label("recipeDescription", Model.of(recipe.getDescription())));
		form.add(new Label("recipeCalories", Model.of(recipe.getCalories())));
		form.add(new Label("recipeFats", Model.of(recipe.getFats())));
		form.add(new Label("recipeProteins", Model.of(recipe.getProteins())));
		form.add(new Label("recipeSaccharides", Model.of(recipe.getSaccharides())));

		Button button = new Button("submitBtn"){
			private static final long serialVersionUID = 678614432694793988L;

			public void onSubmit() {
				UserRecipe userRecipe = new UserRecipe();
				
				Calendar cal = Calendar.getInstance();
				cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),
						cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
				
				userRecipe.setDate(new Date(cal.getTimeInMillis()));
				userRecipe.setUser(userDao.load(((MySession) getSession())
						.getUserID()));
				userRecipe.setUserId(((MySession) getSession())
						.getUserID());
				//TODO WTF
				userRecipe.setDayTime(0);
				userRecipe.setTotalCalories(recipe.getCalories());
				userRecipe.setRecipe(recipe);
				long idRecipe = recipe.getId();
				userRecipe.setRecipeId(idRecipe);
				userRecipeDao.save(userRecipe);
				
				int totalTodayRec = userRecipeDao.sumUserToday(((MySession)getSession()).getUserID());
				int totalTodayFood = userFoodDao.sumUserToday(((MySession)getSession()).getUserID());
				InOutHelper todayHelper = inOutHelperDao.findByUserToday(((MySession)getSession()).getUserID());
				if(todayHelper!=null)
					todayHelper.setCaloriesIn(totalTodayRec+totalTodayFood);
				else {
					InOutHelper newHelper = new InOutHelper();
					newHelper.setDate(new Date(cal.getTimeInMillis()));
					newHelper.setCaloriesIn(totalTodayRec+totalTodayFood);
					newHelper.setUsrid(((MySession)getSession()).getUserID());
					inOutHelperDao.save(newHelper);
				}
				
				info("Recept bol pridany k zjedenym");
				
				userDao.load(((MySession) getSession()).getUserID())
						.getRecipes().add(userRecipe);
				recipe.getUsers().add(userRecipe);
			};
		};
		
		if (((MySession)getSession()).hasPermission()) {
			button.setVisible(true);
		} else {
			button.setVisible(false);
		}
		
		form.add(button);
		
		add(form);
	}

	@Override
	public String getPageTitle() {
		if(recipe != null){
			return "chudnite-zdravo.com - " + recipe.getName();
		}
		return "chudnite-zdravo.com";
	}
}
